package com.google.code.avajhelpdesk.model;

import java.util.Iterator;

import org.apache.wicket.injection.web.InjectorHolder;
import org.apache.wicket.markup.repeater.data.IDataProvider;
import org.apache.wicket.model.IModel;
import org.apache.wicket.spring.injection.annot.SpringBean;

import com.google.code.avajhelpdesk.entities.DomainModel;
import com.google.code.avajhelpdesk.persistence.IGenericDAO;


public class GenericDataProvider<T extends DomainModel> implements IDataProvider {

    @SpringBean(name="genericDAO")
    private IGenericDAO dao;
    private Class<T> entityClass;

    public GenericDataProvider(Class<T> entityClass) {
        this.entityClass = entityClass;
        InjectorHolder.getInjector().inject(this);
    }

    public Iterator iterator(int first, int count) {
        return dao.findAll(entityClass, first, count).iterator();
    }

    public IModel model(Object object) {
        return new GenericLoadableDetachableModel<T>((T) object);
    }

    public int size() {
        return dao.count(entityClass);
    }

    public void detach() {}
}